package com.ddyx.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddyx.entity.EventLog;
import com.ddyx.entity.EventPostView;
import com.ddyx.entity.Post;
import com.ddyx.entity.PostForward;
import com.ddyx.entity.PostPic;
import com.ddyx.entity.Store;
import com.ddyx.entity.user.UStore;
import com.ddyx.mapper.EventLogMapper;
import com.ddyx.mapper.EventPostLikeMapper;
import com.ddyx.mapper.EventPostViewMapper;
import com.ddyx.mapper.PostForwardMapper;
import com.ddyx.mapper.PostMapper;
import com.ddyx.mapper.PostPicMapper;
import com.ddyx.mapper.StoreMapper;
import com.ddyx.mapper.user.UStoreMapper;
import com.ddyx.service.IPostService;
import com.ddyx.util.EventTag;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.PageUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Slf4j
@Service
public class PostServiceImpl implements IPostService {

	@Resource
	PostMapper postMapper;
	@Resource
	PostForwardMapper pfMapper;
	@Resource
	PostPicMapper postPicMapper;
	@Resource
	StoreMapper storeMapper;
	@Resource
	UStoreMapper usMapper;
	@Resource
	EventPostLikeMapper eplMapper;
	@Resource
	EventPostViewMapper epvMapper;
	@Resource
	EventLogMapper eventLogMapper;
	
	
	@Override
	public ResponseForm findPostList(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param.getData()) {
			try {
				Map paramMap = (Map) param.getData();
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
				List<Map> posts = new ArrayList<>();
				PostPic postPic = new PostPic();
				List<Map> postPics = new ArrayList<>();
				String storeNo = paramMap.get("storeNo") + "";
//				Store store = new Store();
//				store.setStoreNo(storeNo);
//				store = storeMapper.selectOne(store);
				UStore store = new UStore();
				store.setStoreNo(storeNo);
				store = usMapper.selectOne(store);
				
				paramMap.put("storeNo", store.getStoreNo());
				paramMap.put("isOther", store.getIsOther());
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				posts = postMapper.findPostList(paramMap);
				
				if (!posts.isEmpty()) {
					for (Map map : posts) {
						int postId = (int) map.get("postId");
						postPic.setPostId(postId);
						postPics = postPicMapper.selectPostPicAndgoodsInfo(postPic);
						map.put("postPics", postPics);
					}
				}
				result.setTotal((int) pageHelper.getTotal());
				result.setData(posts);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getPostDetail(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param.getData()) {
			try {
				Map paramMap = (Map) param.getData();
				Integer postId = (Integer) paramMap.get("postId");
				Post post = new Post();
				PostPic postPic = new PostPic();
//				Store store = new Store();
				EventPostView epv = new EventPostView();
				postMapper.addViewCount(postId);//帖子浏览数加1
				Map resultMap  = postMapper.getPostDetail(paramMap);
				//这个帖子的图片,和对应的商品的信息
				postPic.setPostId(postId);
				List<Map> postPics = postPicMapper.selectPostPicAndgoodsInfo(postPic);
				resultMap.put("postPics",postPics);
				//点赞人列表
//				List<Map> peoples = eplMapper.selectLikePeople(postId);
//				resultMap.put("peoples", peoples);
				result.setData(resultMap);
				//同步用户浏览表
				epv.setPostId(postId);
				epv.setWxUid(paramMap.get("wxUid")+"");
				epv.setEventTime(new Date());
				epvMapper.insertSelective(epv);
				
				//同步事件表
				EventLog eventLog = new EventLog();
				eventLog.setEventTag(EventTag.VIEWPOST);
				eventLog.setEventTime(new Date());
				eventLog.setStoreNo(post.getStoreNo());
				eventLog.setWxUid(paramMap.get("wxUid")+"");
				eventLogMapper.insertSelective(eventLog);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Transactional
	@Override
	public ResponseForm sendPost(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(null!=param.getData()) {
			try {
				Map paramMap = (Map) param.getData();
				Post post = new Post();
				Date date = new Date();
				MyBeanUtils.populate(post, paramMap);
				post.setCreateTime(date);
				postMapper.insertSelective(post);
				if (null!=post.getPostId()) {
					PostPic postPic = new PostPic();
					List<Map> postPics = (List<Map>) paramMap.get("postPics");
					for (Map m : postPics) {
						MyBeanUtils.populate(postPic, m);
						postPic.setPostId(post.getPostId());
						postPicMapper.insertSelective(postPic);
						postPic.setPostPicId(null);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm androidPosts(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param.getData()) {
			try {
				Map paramMap = (Map) param.getData();
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
//				Store store = new Store();
				List<Map> posts = new ArrayList<>();
				PostPic postPic = new PostPic();
				List<Map> postPics = new ArrayList<>();
//				String storeNo = paramMap.get("storeNo") + "";
//				store.setStoreNo(storeNo);
//				store = storeMapper.selectOne(store);
//				paramMap.put("isOther", store.getIsOther());
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				posts = postMapper.androidPosts(paramMap);
				
				if (!posts.isEmpty()) {
					for (Map map : posts) {
						int postId = (int) map.get("postId");
						postPic.setPostId(postId);
						postPics = postPicMapper.selectPostPicAndgoodsInfo(postPic);
						map.put("postPics", postPics);
						
						//转发人头像
						List<Map> forwardEmployes =  pfMapper.selectEmployes(postId);
						map.put("employes", forwardEmployes);
					}
				}
				result.setTotal((int) pageHelper.getTotal());
				result.setData(posts);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm androidPostForward(RequestForm param) {
		ResponseForm result = new ResponseForm();
			try {
				Map paramMap = (Map) param.getData();
				Integer flag = pfMapper.newForward(paramMap);
				if(flag==0) {
					result.setMessage("已转发过");
				}
				else {
					result.setMessage("转发成功");
				}
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		return result;
	}


}
